package com.cheers.action;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;

import com.cheers.common.Constants;
import com.cheers.common.ResultModel;
import com.cheers.service.UserService;
import com.cheers.util.VerifyUtil;

public class UserAction extends BaseAction {

	private static final long serialVersionUID = 8388146173252690443L;

	@Resource
	private UserService userService;
	
	private String userId;
	private String username;
	private String password;
	private String email;
	private String address;
	private String mobile;
	
	private String realname;
	private String qq;
	private String job;
	
	
	/**
	 * 申请推广员
	 * @return
	 * @throws Exception
	 */
	public String apply() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(realname)) {
			result = new ResultModel(Constants.NO, Constants.REALNAME_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(mobile)) {
			result = new ResultModel(Constants.NO, Constants.MOBILE_DESC, "");
			return SUCCESS;
		}
		if (!VerifyUtil.numberTest(mobile)) {
			result = new ResultModel(Constants.NO, Constants.MOBILE_INVALID_DESC, "");
			return SUCCESS;
		}
		if (!isPost("apply")) {
			result = new ResultModel(Constants.NO, Constants.INVALID_DESC, "");
			return SUCCESS;
		}
		
		//申请
		String applyId = userService.apply(userId, realname, address, mobile, qq, job);
	    if (Constants.USER_CODE.equals(applyId)) {
			result = new ResultModel(Constants.USER_CODE, Constants.USER_DESC, "");
			return SUCCESS;
		}
		result = new ResultModel(applyId);
		return SUCCESS;
	}
	
	/**
	 * 修改用户信息
	 * @return
	 * @throws Exception
	 */
	public String modUser() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (!isPost("modUser")) {
			result = new ResultModel(Constants.NO, Constants.INVALID_DESC, "");
			return SUCCESS;
		}
		
		//修改用户信息
		String code = userService.mod(userId, password, address, mobile);
		if (Constants.OK.equals(code)) {
			result = new ResultModel("");
		} else if (Constants.USER_CODE.equals(code)) {
			result = new ResultModel(code, Constants.USER_DESC, "");
		} else {
			result = new ResultModel(Constants.NO, Constants.NO_DESC, "");
		}
		
		return SUCCESS;
	}
	
	/**
	 * 登陆
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		//验证
		if (StringUtils.isEmpty(username)) {
			result = new ResultModel(Constants.NO, Constants.USERNAME_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(password)) {
			result = new ResultModel(Constants.NO, Constants.PASSWOR_DESC, "");
			return SUCCESS;
		}
		if (!isPost("login")) {
			result = new ResultModel(Constants.NO, Constants.INVALID_DESC, "");
			return SUCCESS;
		}
		
		//是否为注册用户
		String userId = userService.login(username, password);
		if (userId != null) {
			result = new ResultModel(userId);
		} else {
			result = new ResultModel(Constants.NO, "登陆失败", "");
		}
		
		return SUCCESS;
	}
	
	/**
	 * 注册
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {
		//验证
		if (StringUtils.isEmpty(username)) {
			result = new ResultModel(Constants.NO, Constants.USERNAME_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(password)) {
			result = new ResultModel(Constants.NO, Constants.PASSWOR_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(email)) {
			result = new ResultModel(Constants.NO, Constants.EMAIL_DESC, "");
			return SUCCESS;
		}
		String test = VerifyUtil.mailTest(email);
		if (!Constants.OK.equals(test)) {
			result = new ResultModel(Constants.NO, test, "");
			return SUCCESS;
		}
		if (!isPost("register")) {
			result = new ResultModel(Constants.NO, Constants.INVALID_DESC, "");
			return SUCCESS;
		}
		//验证用户名唯一性
		boolean exist = userService.exist(username);
		if (exist) {
			result = new ResultModel(Constants.NO, "用户名已被占用", "");
			return SUCCESS;
		}
		
		//注册
		String userId = userService.register(username, password, email);
		result = new ResultModel(userId);
		return SUCCESS;
	}

	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	public String getRealname() {
		return realname;
	}
	public void setRealname(String realname) {
		this.realname = realname;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	
}
